﻿using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;

namespace DreamBuilder.UI.UserControls
{
    [DefaultProperty("Text"), ToolboxData("<{0}:ListGridView runat=server></{0}:ListGridView>")]
    public class ListGridView : System.Web.UI.WebControls.GridView
    {
        private bool enableEmptyContentRender = true;
        /// <summary>
        /// 是否数据为空时显示标题行
        /// </summary>
        public bool EnableEmptyContentRender
        {
            set { enableEmptyContentRender = value; }
            get { return enableEmptyContentRender; }
        }

        private string emptyDataCellCssClass;
        /// <summary>
        /// 为空时信息单元格样式类
        /// </summary>
        public string EmptyDataCellCssClass
        {
            set { emptyDataCellCssClass = value; }
            get { return emptyDataCellCssClass; }
        }

        /// <summary>
        /// 为空时输出内容
        /// </summary>
        /// <param name="writer"></param>
        protected virtual void RenderEmptyContent(HtmlTextWriter writer)
        {
            Table table = new Table(); //create a table
            table.CssClass = this.CssClass; //copy all property
            table.GridLines = this.GridLines;
            table.BorderStyle = this.BorderStyle;
            table.BorderWidth = this.BorderWidth;
            table.CellPadding = this.CellPadding;
            table.CellSpacing = this.CellSpacing;
            table.HorizontalAlign = this.HorizontalAlign;

            table.Width = this.Width;

            table.CopyBaseAttributes(this);

            TableRow row = new TableRow();

            row.CssClass = this.HeaderStyle.CssClass;

            table.Rows.Add(row);
            int showCols = 0;
            foreach (DataControlField dataControlField in this.Columns) //generate table header
            {
                if (dataControlField.Visible == true)
                {
                    showCols++;
                    TableCell cell = new TableCell();

                    cell.Text = "";


                    if (dataControlField is BoundField)
                    {
                        cell.Text = ((BoundField)dataControlField).HeaderText;
                    }
                    else if (dataControlField is TemplateField)
                    {
                        cell.Text = ((TemplateField)dataControlField).HeaderText;
                    }
                    else
                    {
                        cell.Text = dataControlField.HeaderText;
                    }


                    cell.CssClass = dataControlField.HeaderStyle.CssClass;// "CL-childTable-head"; //这里把表头样式写死了

                    row.Cells.Add(cell);
                }
            }

            TableRow row2 = new TableRow();
            table.Rows.Add(row2);

            TableCell msgCell = new TableCell();
            msgCell.CssClass = this.emptyDataCellCssClass;

            if (this.EmptyDataTemplate != null) //the second row, use the template
            {
                this.EmptyDataTemplate.InstantiateIn(msgCell);
            }
            else //the second row, use the EmptyDataText
            {
                if (this.EmptyDataText != string.Empty)
                {
                    msgCell.Text = this.EmptyDataText;
                }
                else
                {
                    msgCell.Text = "<div style='text-align:center;'><span style='color:red;'>没有数据</span></div>";
                }
            }

            msgCell.HorizontalAlign = HorizontalAlign.Center;
            msgCell.ColumnSpan = showCols;

            row2.Cells.Add(msgCell);
            table.RenderControl(writer);
        }

        protected override void OnRowDataBound(GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.EmptyDataRow)
            {
                //更改选择行的颜色
                e.Row.Attributes.Add("onclick", "javascript:return ClickSetColor(this);");
                e.Row.Attributes.Add("onmouseover", "javascript:MouseOnSetColor(this);");
                e.Row.Attributes.Add("onmouseout", "javascript:MouseOutCancelColor(this);");
            }
            base.OnRowDataBound(e);
        }
        protected override void Render(HtmlTextWriter writer)
        {
            if (enableEmptyContentRender && (this.Rows.Count == 0 || this.Rows[0].RowType == DataControlRowType.EmptyDataRow))
            {
                RenderEmptyContent(writer);
            }
            else
            {
                base.Render(writer);
            }
        }
    }
}